<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Model_Refund extends Zend_Db_Table{
	
	protected $_name    = "refund";
    protected $_primary = "id";
    
    public function saveData($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            if(isset($data['order_id'])){
                $id = $data['order_id'];
            
                $db->delete('refund', "order_id = $id");
            }
            
            $db->insert('refund', $data);
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function savePayment($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->insert('payment', $data);
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function getPayments($customer_id){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('p' => 'payment'), array('id', 'total', 'date'))
                    ->order("p.id")
                    ->where('p.customer_id = ?', $customer_id, 'INTEGER');
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getPayment($customer_id = null){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('p' => 'payment'), array('customer_id', 'SUM(total) AS total'));
        
        if($customer_id != null){
            $select->where('p.customer_id = ?', $customer_id, 'INTEGER');
            $result = $db->fetchRow($select);
        }else{
            $select->group('p.customer_id');
            $result = $db->fetchAll($select);
        }
        
        return $result;
    }
    
    public function getDatas(){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('r' => 'refund'), array('SUM(total) AS total'))
                    ->joinRight(array('c' => 'customers'), "c.id = r.customer_id", array('name', 'id as customer_id'))
                    ->group("c.id")
                    ->order("c.name");
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getData($customer_id){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('r' => 'refund'), array('id', 'total', 'date'))
                    ->joinRight(array('c' => 'customers'), "c.id = r.customer_id", array('name', 'id as customer_id'))
                    ->order("r.id DESC")
                    ->where("c.id = ?", $customer_id, "INTEGER");
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getDataByOrder($order_id){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('r' => 'refund'), array('id', 'total', 'date'))
                    ->joinRight(array('c' => 'customers'), "c.id = r.customer_id", array('name', 'id as customer_id'))
                    ->order("r.id DESC")
                    ->where("r.order_id = ?", $order_id, "INTEGER");
        
        $result = $db->fetchRow($select);
        
        return $result;
    }
    
    public function deleteData($id){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->delete("refund", "id = $id");
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function checkOrderExists($order_id){
        $option = array('table' => 'refund', 'field' => 'order_id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
}